# # -*- encoding: utf-8 -*-
# """
# File Name: alarm_sender
# Description:
# Author : 'lirui'
# date: 2024/2/8
# Contact: marjey_lee@163.com
# Change Activity:
# 2024/2/8:
# """
# import json
# import time
#
# import ll
# from modules.blk.alarm.blk_alert import BLKAlert
# from projects.monitoring.alarm_manager.services.redis_alarm_manager import REDIS_ALARM_MANAGER
# from projects.monitoring.alarm_manager.utils.elastic_info_sender import send_data_to_elastic
# from projects.monitoring.common.do.alarm import Alarm
#
#
# class AlarmSender:
#     @staticmethod
#     def _format_alarm(alarm: Alarm):
#         """
#         ["remind", "warning", "fatal"]
#         """
#         level = alarm.severity
#         host_name = alarm.hostname
#         ip = alarm.ip
#         if host_name is None or len(host_name) == 0:
#             host_name = ip
#         alarm_name = f'{host_name}-{alarm.alarm_info}'
#         if alarm.manager_ip is not None and alarm.manager_port is not None:
#             alarm_content = f'{alarm_name}—Manager:{alarm.manager_ip}:{alarm.manager_port}'
#         else:
#             alarm_content = alarm_name
#         return level, ip, alarm_name, alarm_content
#
#     @staticmethod
#     def _check_is_in_interval(alarm: Alarm):
#         alarm_id = alarm.alarm_id
#         content = REDIS_ALARM_MANAGER.get(key=alarm_id)
#         if content is None:
#             return False
#         content = json.loads(str(content, 'utf8'))  # filter
#         if abs(time.time() - content['last_send_time']) < 60 * 60 * 24 * 3:
#             return True
#         return False
#
#     @staticmethod
#     def _refresh_redis_history(alarm: Alarm):
#         alarm_id = alarm.alarm_id
#         alarm.last_send_time = time.time()
#         REDIS_ALARM_MANAGER.set(key=alarm_id, value=alarm.to_json())
#
#     @staticmethod
#     def _send(level, ip, alarm_name, alarm_content, action, alarm: Alarm):
#         """
#
#         Args:
#             level:
#             ip:
#             alarm_name:
#             alarm_content:
#             action:
#             alarm:
#
#         Returns:
#
#         """
#         if level == 'remind':
#             return
#         alarm_sender = BLKAlert()
#         alarm_sender.send_internal_alarm(
#             ip=ip,
#             level=level,
#             alarm_name=alarm_name,
#             alarm_content=alarm_content,
#             action=action,
#         )
#         AlarmSender._refresh_redis_history(alarm)
#
#     @staticmethod
#     def _send_to_elastic(problems):
#         """
#         send to elastic.
#         Args:
#             problems:
#
#         Returns:
#
#         """
#
#         elastic_index = f'problems'
#         for k, p in problems.items():
#             content = p.to_json()
#             content['alarm_send'] = True
#             ll.get_logger().info(f'Send {content} to elastic.')
#             doc_id = p.alarm_id
#             send_data_to_elastic(elastic_index, doc_id, content)
#
#     @staticmethod
#     def send_alarm(problems):
#         """
#         send alarm
#         """
#         filter_problems = {}
#         to_send_problems = {}
#         for k, alarm in problems.items():
#             if alarm.severity == 'remind':
#                 filter_problems[k] = alarm
#             else:
#                 to_send_problems[k] = alarm
#         AlarmSender._send_problems(
#             to_send_problems,
#             action='firing'
#         )
#
#     @staticmethod
#     def _send_problems(alarms: list[Alarm], action):
#         """
#         send problems
#         Args:
#             alarms:
#             action:
#
#         Returns:
#
#         """
#         for alarm in alarms:
#             level, ip, alarm_name, alarm_content = AlarmSender._format_alarm(alarm)
#             AlarmSender._send(
#                 level,
#                 ip,
#                 alarm_name,
#                 alarm_content,
#                 action,
#                 alarm
#             )
#         # AlarmSender._send_to_elastic(problems)
#
#     @staticmethod
#     def send_recovery(problems):
#         """
#         send recovery
#         """
#         AlarmSender._send_problems(problems, action='resolved')
